package com.ruoyi.cmdb.util;

import com.jcraft.jsch.*;
import lombok.SneakyThrows;

import java.io.*;

//定义用ssh"密码"scp文件的工具类
public class SshScpByPassword {
    @SneakyThrows
    public static void send(String host,
                            Integer port,
                            String user,
                            String password,
                            String localFilePath,
                            String remoteFilePath){
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;

        try {
            // Set up session
            session = jsch.getSession(user, host, port);
            session.setPassword(password);

            // Avoid asking for key confirmation
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            // Connect
            session.connect();

            // Open SFTP channel
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // Copy file
            File localFile = new File(localFilePath);
            try (InputStream inputStream = new FileInputStream(localFile)) {
                channelSftp.put(inputStream, remoteFilePath);
                //channelSftp.chmod(777, remoteFilePath);
            }

            System.out.println("File copied successfully!");

        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }

    @SneakyThrows
    public static void get(String host,
                           Integer port,
                           String user,
                           String password,
                           String remoteFilePath,
                           String localFilePath){
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;

        try {
            // Set up session
            session = jsch.getSession(user, host, port);
            session.setPassword(password);

            // Avoid asking for key confirmation
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            // Connect
            session.connect();

            // Open SFTP channel
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // Download file
            try (OutputStream outputStream = new FileOutputStream(new File(localFilePath))) {
                channelSftp.get(remoteFilePath, outputStream);

            }
            System.out.println("File get successfully!");
        } catch (JSchException | SftpException | IOException e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }
}
